﻿using RSFrame.Services;
using RSModels;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Windows;

namespace AccountService
{
    class AccountService : IAccountService
    {
        public IDBService DBService { get; private set; }

        public AccountService(IDBService dBService)
        {
            DBService = dBService;
        }

        public User GetUserInstance(int account, string password)
        {
            User result = null;
            DbConnection c = DBService.EstablishConnection();
            DbDataReader a = DBService.ReadData(c, "exec dbo.get_user_info @Account=@_account, @Password=@_password",
                new Dictionary<string, object> { { "@_account", account }, { "@_password", password } });
            if (a.Read())
            {
                result = new User()
                {
                    Account = account,
                    Name = (string)a[1],
                    Password = password
                };
            }
            DBService.CloseConnection(c);
            return result;
        }

        public User Login(int account, string password)
        {
            try
            {
                User result = null;
                DbConnection c = DBService.EstablishConnection();
                DbDataReader a = DBService.ReadData(c, "exec dbo.check_user @Account=@_account, @Password=@_password",
                    new Dictionary<string, object> { { "@_account", account }, { "@_password", password } });
                if (a.Read())
                    result = GetUserInstance(account, password);
                DBService.CloseConnection(c);
                return result;
            }
            catch
            {
                return null;
            }
        }

    }
}
